package com.kuang.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.kuang.pojo.User;
import com.kuang.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// @Controller
@RestController  // 代替@ResponseBody 不会走视图解析器，会直接返回一个字符串，前后端分离时候用
public class UserController {
    @RequestMapping(value = "/j1", produces = "application/json;charset=utf-8")
    // @ResponseBody  // 不会走视图解析器，会直接返回一个字符串
    public String json1() throws JsonProcessingException {
        // 创建一个对象
        User user = new User("秦疆1号", 3, "男");
        // return user.toString();
        // jackson ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        String msg = mapper.writeValueAsString(user);
        return msg;
    }

    @RequestMapping(value = "/j2", produces = "application/json;charset=utf-8")
    public String json2() throws JsonProcessingException {
        List<User> userList = new ArrayList<>();
        // 创建一个对象
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        // return user.toString();
        // jackson ObjectMapper
        // ObjectMapper mapper = new ObjectMapper();
        // String msg = mapper.writeValueAsString(userList);
        // return msg;
        return JsonUtils.getDateAsJson(userList);
    }

    @RequestMapping(value = "/j3", produces = "application/json;charset=utf-8")
    public String json3() throws JsonProcessingException {
        // 第一种方法：手动格式化时间
        /* Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // jackson ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(sdf.format(date));*/

        // 第二种方法：使用mapper配置输出格式化时间
        // Date date = new Date();
        // // jackson ObjectMapper
        // ObjectMapper mapper = new ObjectMapper();
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //
        // mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        // mapper.setDateFormat(sdf);
        //
        // return mapper.writeValueAsString(date);

        // 第三种方法： 编写工具类
        Date date = new Date();
        return JsonUtils.getDateAsJson(date, "yyyy-MM-dd HH:mm:ss");
    }
    @RequestMapping(value = "/j4", produces = "application/json;charset=utf-8")
    public String json4() throws JsonProcessingException {
        List<User> userList = new ArrayList<>();
        // 创建对象
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        return JSON.toJSONString(userList);
    }
}
